package com.manong.controller;


import com.manong.aspect.AutoFill;
import com.manong.entity.Department;
import com.manong.service.DepartmentService;
import com.manong.utils.Result;
import com.manong.vo.query.DepartmentQueryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Dorr
 * @since 2025-03-03
 */
@RestController
@RequestMapping("/api/department")
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;

    /**
     *查询部门列表
     * @param departmentQueryVo
     * @return
     */
    @GetMapping("/list")
    public Result List(DepartmentQueryVo departmentQueryVo){
        List<Department> departmentList = departmentService.findDepartmentList(departmentQueryVo);
        return Result.ok(departmentList);
    }

    /**
     * 查询上级部门列表
     * @return
     */
    @GetMapping("/parent/list")
    public Result getParentDepartment(){
        //调用查询上级部门列表方法
        List<Department> departmentList = departmentService.findParentDepartment();
        //返回数据
        return Result.ok(departmentList);
    }

    /**
     * 添加部门
     * @param department
     * @return
     */
    @AutoFill(menu = "部门管理", type = "添加")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:department:add')")
    public Result add(@RequestBody Department department){
        //调用添加部门方法
        if(departmentService.save(department)){
            return Result.ok().message("添加部门成功");
        }
        //返回数据
        return Result.error().message("添加部门失败");
    }

    /**
     * 修改部门
     * @param department
     * @return
     */
    @AutoFill(menu = "部门管理", type = "修改")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('sys:department:edit')")
    public Result update(@RequestBody Department department){
        //调用修改部门方法
        if(departmentService.updateById(department)){
            return Result.ok().message("修改部门成功");
        }
        //返回数据
        return Result.error().message("修改部门失败");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @AutoFill(menu = "部门管理", type = "删除")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:department:delete')")
    public Result delete(@PathVariable Long id){
        //调用删除部门方法
        if(departmentService.removeById(id)){
            return Result.ok().message("删除部门成功");
        }
        //返回数据
        return Result.error().message("删除部门失败");
    }

    /**
     * 检查部门下是否有员工或子部门
     * @param id
     * @return
     */
    @GetMapping("/check/{id}")
    @PreAuthorize("hasAuthority('sys:department:delete')")
    public Result check(@PathVariable Long id){
        if(departmentService.hasUserOfDepartment(id)){
            return Result.exist().message("该部门下有员工，不能删除");
        }
        if(departmentService.hasChildrenOfDepartment(id)){
            return Result.exist().message("该部门下有子部门，不能删除");
        }
        return Result.ok();
    }
}

